/**
 * 
 */
package com.edocbase.desktop.plugins.control;

import org.eclipse.draw2d.BorderLayout;
import org.eclipse.draw2d.Figure;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import com.edocbase.desktop.lightweight.widgets.ControlFigure;
import com.edocbase.desktop.lightweight.widgets.FigureOwner;
import com.edocbase.desktop.lightweight.widgets.TabFolder;
import com.edocbase.desktop.lightweight.widgets.TabItem;
import com.edocbase.desktop.plugins.mdiary.MDiaryUI;
import com.edocbase.desktop.swt.widgets.RichBrowser;

/**
 * @author X61
 * 
 */
public class RichBrowserTabItem extends TabItem {
	private MDiaryUI diaryUI = null;
	private RichBrowser richBrowser;
	private ControlFigure<Composite> browserFigure;
	HTMLRichViewer htmlViewer = null;
	private String url = null;

	public RichBrowserTabItem(MDiaryUI diaryUI, TabFolder folder, int style) {
		super(folder, style);
		this.diaryUI = diaryUI;
		body = new FigureOwner();
		body.setLayoutManager(new BorderLayout());
	}

	@Override
	public Figure getBody() {
		if (browserFigure == null) {
			if (body == null) {
				return null;
			}
			Composite bodyControl = crateBody(folder.getOwner());
			browserFigure = new ControlFigure<Composite>();
			browserFigure.setControl(bodyControl);
			body.add(browserFigure, BorderLayout.CENTER);
		}
		return super.getBody();
	}

	public void setDiaryUI(MDiaryUI diaryUI) {
		this.diaryUI = diaryUI;
	}

	protected Composite crateBody(Control parent) {
		if (htmlViewer == null) {
			Composite container = (Composite) parent;
			htmlViewer = new HTMLRichViewer(container, SWT.NONE);
			richBrowser = htmlViewer.getRichBrowser();
			richBrowser.addTitleListener(new TitleListener() {
				@Override
				public void changed(TitleEvent event) {
					String title = event.title;
					RichBrowserTabItem.this.setText(title);
				}
			});
			new BrowserOpenListener(diaryUI, htmlViewer);
			// htmlViewer.getRichBrowser().addOpenWindowListener(openListener);
			// htmlViewer.getRichBrowser().addLocationListener(openListener);
			if (url != null) {
				container.getDisplay().asyncExec(new Runnable() {

					@Override
					public void run() {
						htmlViewer.open(url);
						htmlViewer.getRichBrowser().setVisible(true);
					}
				});

			} else {
				// htmlViewer.open();
			}

		}
		return htmlViewer.getRichBrowser().getBrowser();
	}

	public String getUrl() {
		if (url == null && richBrowser != null) {
			url = richBrowser.getUrl();
		} else if (url != null && url.equals("about:blank")
				&& richBrowser != null) {
			url = richBrowser.getUrl();
		} else {
			url = richBrowser.getUrl();
		}
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public RichBrowser getRichBrowser() {
		return richBrowser;
	}

	@Override
	public void dispose() {
		super.dispose();
		if (browserFigure != null) {
			browserFigure.dispose();
			browserFigure = null;
		}
	}
}
